package com.mkait.thief.util;

import com.alibaba.fastjson.JSONObject;
import com.mkait.thief.bean.vo.MkaitTokenBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@Component
@Slf4j
public class HttpUtils {

    @Autowired
    private MkaitTokenBean mkaitTokenBean;


    public boolean sendGet() {
        return false;
    }

    public boolean sendPost(String urlStr,JSONObject json,String successMsg) {
        HttpURLConnection connection = null;
        OutputStream out = null;
        InputStream inputStream = null;
        try {
            URL url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(3000);
            // 限制输入流等待数据到达的时间，超时将会抛出java.net.SocketTimeoutException
            connection.setReadTimeout(3000);
            // 设定请求的方法为"POST"，默认是GET
            connection.setRequestMethod("POST");
            // 设置传送的内容类型是json格式
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            // 接收的内容类型也是json格式
            connection.setRequestProperty("Accept", "application/json;charset=utf-8");
            // 设置是否从httpUrlConnection读入，默认情况下是true
            connection.setDoInput(true);
            // 由于URLConnection在默认的情况下不允许输出，所以在请求输出流之前必须调用setDoOutput(true)。为一个HTTP URL将doOutput设置为true时，请求方法将由GET变为POST
            connection.setDoOutput(true);
            // 是否使用缓存，Post方式不能使用缓存
            connection.setUseCaches(false);
            // 设置Token
            connection.setRequestProperty("cookie",mkaitTokenBean.getToken());
            out = connection.getOutputStream();
            out.write(json.toString().getBytes(StandardCharsets.UTF_8));
            out.flush();
            connection.connect();
            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                inputStream = connection.getInputStream();
                String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining(System.lineSeparator()));
                if (result.indexOf(successMsg) != -1) {
                    return true;
                }
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}
